<?php


	include_once("cmsConfig.php");
	include_once("CmsStatusMessage.php");
	//include_once("imagesModule.inc");
	
	/**
		class to provide cms write/update functionality
	*/
	

	class CmsReplicator {
		
		var $formId;
		var $documentCode;
		var $documentCodeSave;
		var $document;
		var $authInfo;
		var $db;
		var $replication;
		
		//table writers - lazy loaded as needed//
		var $contentTableWriter;
		var $imageTableWriter;
		var $manyToManyTableWriter;
		var $binaryDataTableWriter;
		var $formDescriptorTableWriter;
		
		function CmsReplicator( $documentCode, &$authInfo, &$db) {
			$this->documentCode = $documentCode;
			$this->authInfo  = &$authInfo;
			$this->db = &$db;	
//			$this->formDescriptorTableWriter = $this->getFormDescriptorTableWriter();
//			$this->manyToManyTableWriter = $this->getManyToManyTableWriter();
//			$this->binaryDataTableWriter = $this->getBinaryDataTableWriter();
//			$this->imageTableWriter = $this->getImageTableWriter();
//			$this->contentTableWriter = $this->getContentTableWriter();
//			$this->document = $this->getDocument();// write a function for generating this		
//			$this->formId = $this->document->formid;
			//$this->replication = $this->replicate();
		}
		
		function &getContentTableWriter(){
			if ( !isset($this->tableWriter)){
				$this->contentTableWriter = new CmsTableWriter(CMS_FORMAT, $this->db);
			}
			return $this->contentTableWriter;
		}
			
	
		function &getImageTableWriter() {
			if (! isset($this->imageTableWriter) ){
				$this->imageTableWriter = new CmsTableWriter(CMS_IMAGE_DESCRIPTOR ,$this->db);
			}
			return $this->imageTableWriter;
		}
		
		function &getManyToManyTableWriter() {
			if (! isset($this->manyToManyTableWriter)){
				$this->manyToManyTableWriter = new CmsTableWriter(CMS_MANYDATA, $this->db);
			}
			return $this->manyToManyTableWriter;
		}
		
		function &getBinaryDataTableWriter() {
			if(! isset( $this->binaryDataTableWriter)){
				$this->binaryDataTableWriter = new CmsTableWriter(CMS_BINARYDATA, $this->db);
			}
			return $this->binaryDataTableWriter;
		}
	
		function &getFormDescriptorTableWriter() {
			if(! isset( $this->formDescriptorTableWriter)){
				$this->formDescriptorTableWriter = new CmsTableWriter(CMS_FORM_DESC_FORMAT, $this->db);
			}
			return $this->formDescriptorTableWriter;
		}
		
		function getDocument() {
			if(! isset( $this->document)) {
				$tw = $this->getContentTableWriter();
				$this->document = $tw->readItem($this->documentCode);
			}
			return $this->document;	
		}
		
		function replicate() {
		//	if(! isset($this->replication)) {
		
			if ( $tempCopy = $this->getDocument() ) {
				
				$tempCopy = (array) $tempCopy;
				
				$this->documentCode = $tempCopy['documentcode'];
				$this->formId = $tempCopy['formid'];
				
				$fdtw = $this->getFormDescriptorTableWriter();
				$formDescription = $fdtw->readItem($this->formId);
				
				$element = new CmsFormElement($formDescription, $this->db);
				
				//$this->replication = new stdClass();//hate arrays... i get an object i want to return one?
				$this->replication = array();
				
				unset( $tempCopy['documentcode']);
				unset( $tempCopy['formid']);//we actually need this..
				
				//iterate throught the data..
				foreach ($tempCopy as $key=>$value) {
					
					//need to deal to things..
					if((!is_null($value) && $value != "")){// && $key != "documentcode" && $key != "formid") {
						
						$e = $element->getElementDescriptor($formDescription->$key);

						$name = $e->name;

						if($e->elementtype == CMS_ELEMENT_CHECKBOX_LIST) {
							// Okay pull out the many to many data and reformat it.
							
							//workout if it is checked
							$checked = array();
							$this->db->executeQuery("SELECT relatedcode FROM manytomanydata WHERE parentcode='".$this->documentCode."' ");
								while (($checkRow = $this->db->nextRow())){
									$checked[$checkRow[0]] = $checkRow[0];
								}
							//echo "checked array = <pre>";print_r($checked);echo "</pre><br>";
								
							//checkBoxList($e->name,magicClean($e->sourcequery, 0), $this->db, $checked); $name .= "[]";
							$this->db->executeQuery(magicClean($e->sourcequery, 0));
							$manyData = '';
							while (($row = $this->db->nextRow())){
								//echo "row = <pre>";print_r($row);echo "</pre><br>";
								if(isset($checked[$row[0]])) {
									$manyData[] = $row[0];
								}
							}
							$this->replication[$key] = $manyData;
							
						} elseif ($e->elementtype == CMS_ELEMENT_FILE_UPLOAD){
							
							//the table writer is too heavy here as it reads in the whole row.. which we dont need.
							$query = "SELECT id,documentcode,elementid,filetypecode,filesize,mimetype,filename FROM binarydata where id='".$this->document->$name."' AND filetypecode='1'";
							$this->db->executeQuery($query);
							$row = $this->db->nextObject();
							
							if ($row){
								$this->replication[$key] = $row;
							}
							//get the data out of the db
							//$btw = $this->getBinaryDataTableWriter();
							//$btw->resetCustomIterator("WHERE id='".$this->document->$name."' AND filetypecode='1' ");
							//$pictureOutRow = '';
							//while($binaryRow = $btw->nextItem()) {
							//	//set the variables
							//	
							//	//evil hax0r.. this data will not persist across page loads,
							//	// we need to store it to the session..
							//	// but this could be bad...
							//	// so perhaps.. instead.. we store a reference to the record in
							//	// the db .. and suck it out in the cmswrite.
							//	$dataKey = $e->name . $e->elementid;//echo "data Key = $dataKey <br>";
							//	$_FILES[$e->name]['tmp_name'] = "mem://" . $dataKey . "";//echo "ereg = " . ereg("mem://", $_FILES[$e->name]['tmp_name']) . "<br>End<br>";
							//	$GLOBALS[$dataKey ] = &$binaryRow->content;//echo "e name = " . $e->name . "<br>";//echo "data = " . ${ $e->name } . "<br>end<br>";
							//	$_FILES[$e->name]['data'] = $binaryRow->content;
							//	$_FILES[$e->name]['name'] = $binaryRow->filename;
							//	$_FILES[$e->name]['type'] = $binaryRow->mimetype;
							//	$_FILES[$e->name]['size'] = $binaryRow->filesize;
							//}
							
							
						}else {
						
							//set the value
							$this->replication[$key] = $value;
						}
					}
				}
				return $this->replication;
			}
			return null;
		}
	}
?>